const fs = require("fs");

const props = `
{
  // request url 过滤
  requestUrlFilter: ["/v1", "/v2"],
  // api address
  apiUrl: "",
  // api head import
  apiHead: import { request } from '@/services/request',
  // api generate address
  fileFolder: "api/",
  // api typeEnum
  typeEnum: {
    integer: "number",
    int32: "number",
    int64: "number",
    "date-time": "number",
  }
}
`;

async function checkConfigFile() {
  return new Promise((resolve, reject) => {
    try {
      const configContent = fs.readFileSync("api.config.js", "utf-8");
      const config = eval(configContent);
      resolve(config);
    } catch (e) {
      this.readErr = e;
      console.log("文件不存在", e);
      writeSetting();
      reject();
    }
  });
}

function writeSetting() {
  fs.writeFileSync("api.config.js", props, (err) => {
    if (err) {
      return console.log(err);
    }
    console.log("文件写入成功");
  });
}

module.exports = {
  checkConfigFile,
};
